package org.jpro.modules.system.web;

import java.util.Map;

import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jpro.annotation.FormatResponseBodyAnnotation;
import org.jpro.common.web.BaseController;
import org.jpro.modules.system.entity.Dict;
import org.jpro.modules.system.service.IDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 字典controller
 */
@Controller
@RequestMapping("/system/dict")
public class DictController extends BaseController {

    @Autowired
    private IDictService dictService;

    /**
     * 默认页面
     */
    @RequestMapping(method = RequestMethod.GET)
    public String list() {
        return "system/dictList";
    }

    /**
     * 获取字典json
     */
    @ResponseBody
    @RequiresPermissions("sys:dict:view")
    @FormatResponseBodyAnnotation(false)
    @RequestMapping(value = "json", method = RequestMethod.GET)
    public Map<String, Object> getData(String label, String value, String type) {
        return getEasyUIData(dictService.search(label, value, type, super.getPageRequest()));
    }

    /**
     * 添加字典跳转
     */
    @RequiresPermissions("sys:dict:add")
    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String createForm(Map model) {
        model.put("dict", new Dict());
        model.put("action", "create");
        return "system/dictForm";
    }

    /**
     * 添加字典
     */
    @ResponseBody
    @RequiresPermissions("sys:dict:add")
    @RequestMapping(value = "create", method = RequestMethod.POST)
    public void create(@Valid Dict dict) {
        dictService.save(dict);
    }

    /**
     * 修改字典跳转
     */
    @RequiresPermissions("sys:dict:update")
    @RequestMapping(value = "update/{id}", method = RequestMethod.GET)
    public String updateForm(@PathVariable("id") Integer id, Map model) {
        model.put("dict", dictService.get(id));
        model.put("action", "update");
        return "system/dictForm";
    }

    /**
     * 修改字典
     */
    @ResponseBody
    @RequiresPermissions("sys:dict:update")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public void update(@Valid @ModelAttribute @RequestBody Dict dict) {
        dictService.update(dict);
    }

    /**
     * 删除字典
     */
    @ResponseBody
    @RequiresPermissions("sys:dict:delete")
    @RequestMapping(value = "delete/{id}")
    public void delete(@PathVariable("id") Integer id) {
        dictService.delete(id);
    }

    @ModelAttribute
    public void getDict(@RequestParam(value = "id", defaultValue = "-1") Integer id, Map model) {
        if (id != -1) {
            model.put("dict", dictService.get(id));
        }
    }

}
